class Solution {
    public int[] canSeePersonsCount(int[] heights) {
        int n = heights.length;
        int[] answer = new int[n];
        Deque<Integer> stack = new ArrayDeque<Integer>();
        for (int i = n - 1; i >= 0; i--) {
            int height = heights[i];
            while (!stack.isEmpty() && stack.peek() < height) {
                answer[i]++;
                stack.pop();
            }
            if (!stack.isEmpty()) {
                answer[i]++;
            }
            stack.push(height);
        }
        return answer;
    }
}

